package com.x.provider.general.factory.dto;

import com.x.core.utils.BeanUtil;
import com.x.provider.api.general.enums.CategoryTypeEnum;
import com.x.provider.api.general.model.dto.cms.CategoryDTO;
import com.x.provider.api.general.model.dto.cms.CategoryListReqDTO;
import com.x.provider.api.general.model.dto.gift.GiftCategoryDTO;
import com.x.provider.api.general.model.dto.gift.GiftDTO;
import com.x.provider.api.oss.service.OssRpcService;
import com.x.provider.general.model.domain.gift.Gift;
import com.x.provider.general.model.domain.gift.GiftCategory;
import com.x.provider.general.service.cms.CategoryService;
import com.x.provider.general.service.gift.GiftService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author: liushenyi
 * @date: 2022/10/28/16:46
 */
@Service
public class GeneralDTOFactoryImpl implements GeneralDTOFactory{

    private final CategoryService categoryService;
    private final GiftService giftService;
    private final OssRpcService ossRpcService;

    public GeneralDTOFactoryImpl(CategoryService categoryService,
                                 GiftService giftService,
                                 OssRpcService ossRpcService){
        this.categoryService = categoryService;
        this.giftService = giftService;
        this.ossRpcService = ossRpcService;
    }

    @Override
    public List<CategoryDTO> listCategory(CategoryListReqDTO listingDTO) {
        return BeanUtil.prepare(categoryService.listCategory(listingDTO.getCategoryType()), CategoryDTO.class);
    }

    @Override
    public List<GiftCategoryDTO> listGiftCategory() {
        final List<CategoryDTO> categoryList = listCategory(CategoryListReqDTO.builder().categoryType(CategoryTypeEnum.GIFT.getValue()).build());
        final List<Gift> giftList = giftService.listGift();
        final Map<Long, GiftDTO> giftDTOMap = prepareGiftList(giftList).stream().collect(Collectors.toMap(item -> item.getId(), item -> item));
        final Map<Long, List<GiftCategory>> giftCategoryMap = giftService.listGiftCategory().stream().collect(Collectors.groupingBy(GiftCategory::getCategoryId));
        List<GiftCategoryDTO> result = new ArrayList<>(categoryList.size());
        categoryList.stream().forEach(item ->{
            final List<Long> giftIdList = giftCategoryMap.get(item.getId()).stream().map(opt -> opt.getGiftId()).collect(Collectors.toList());
            GiftCategoryDTO giftCategoryDTO = new GiftCategoryDTO();
            giftCategoryDTO.setCategory(item);
            giftCategoryDTO.setGift(new ArrayList<>(giftIdList.size()));
            giftIdList.stream().forEach(opt -> {
                giftCategoryDTO.getGift().add(giftDTOMap.get(opt));
            });
        });
        return result;
    }

    private List<GiftDTO> prepareGiftList(List<Gift> src){
        final List<GiftDTO> result = BeanUtil.prepare(src, GiftDTO.class);
        prepareOssUrl(result);
        return result;
    }

    private void prepareOssUrl(List<GiftDTO> result) {
        List<String> ossId = new ArrayList<>(result.size() * 2);
        result.stream().forEach(opt -> {
            if (!StringUtils.isEmpty(opt.getAnimationId())){
                ossId.add(opt.getAnimationId());
            }
            if (!StringUtils.isEmpty(opt.getIconId())){
                ossId.add(opt.getIconId());
            }
        });
        final Map<String, String> ossUrlMap = ossRpcService.listObjectBrowseUrl(ossId).getData();
        result.stream().forEach(opt -> {
            if (!StringUtils.isEmpty(opt.getAnimationId())){
                opt.setAnimationUrl(ossUrlMap.get(opt.getAnimationId()));
            }
            if (!StringUtils.isEmpty(opt.getIconId())){
                opt.setIconUrl(ossUrlMap.get(opt.getIconId()));
            }
        });
    }

}
